import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import math

# 读取 CSV 文件
df = pd.read_csv("20181024_d1_0830_0900.csv", encoding="utf-8", header=None)

# 使用 ';' 分隔列数据并转换为多个列
df_split = df[0].str.split('; ', expand=True)
print(df_split)

x = []
xavg=[]
y = []
yavg=[]
xaccleration = []
yaccleration = []
accleration = []
zhengfu = []
feature_values = []
speed1 = []
acclerationdif = []
time=[]
transform=[]
time1=[]
xpart=[]
ypart=[]
feature_valuespart = []
realdistance=[]
x_1=23.736448
x_2=23.736544
x_3=23.737832
x_4=23.737739
y_1=37.978852
y_2=37.978933
y_3=37.977328
y_4=37.977251
def calculate_distance(lat1, lon1, lat2, lon2):
    R = 6371  # 地球半径，单位为千米
    rad = lambda angle: angle * math.pi / 180  # 将角度转换为弧度

    lat1_rad = rad(lat1)
    lon1_rad = rad(lon1)
    lat2_rad = rad(lat2)
    lon2_rad = rad(lon2)

    dLat = lat2_rad - lat1_rad
    dLon = lon2_rad - lon1_rad

    a = math.sin(dLat / 2) ** 2 + math.cos(lat1_rad) * math.cos(lat2_rad) * math.sin(dLon / 2) ** 2
    c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))

    distance = R * c  # 返回单位为千米的距离
    return distance
proportion=calculate_distance(y_1, x_1, y_2, x_2)/math.sqrt((y_1-y_2)**2+(x_1-x_2)**2)
k1=(y_1-y_4)/(x_1-x_4)
b1=y_1-k1*x_1
k2=(y_3-y_2)/(x_3-x_2)
b2=y_3-k2*x_3

def dotlinedistance(x, y, k, b):
    linedistance=(k*x+b-y)/(math.sqrt(k**2+1))*proportion
    return linedistance

# 从第二行起提取数据
for i in range(1, len(df_split)):  # 从第二行开始
    for j in range(0, (len(df_split.columns) -4)// 6):  # 根据数据每6列一个周期提取数据
        # 确保x和y是数值类型
        if pd.isnull(df_split.iloc[i, 6 * j + 4]) or (df_split.iloc[i, 6 * j + 4]==''):
            break
        try:
            y_value = float(df_split.iloc[i, 6 * j + 4])  # 尝试将y值转换为float
            y.append(y_value)
        except ValueError:
            print(i)
            print(j)
            print(df_split.iloc[i, 6 * j + 4])
        x.append(float(df_split.iloc[i, 6 * j + 5]))  # 获取x坐标并转换为float
        time.append(float(df_split.iloc[i, 6 * j + 9]))  # 获取x坐标并转换为float
        # 将加速度值转换为float类型
        xaccleration.append(float(df_split.iloc[i, 6 * j + 7]))  
        yaccleration.append(float(df_split.iloc[i, 6 * j + 8]))  
        # 将速度值转换为float类型
        speed1.append(float(df_split.iloc[i, 6 * j + 6]))  # 获取特征值
    transform.append(len(x))

# 计算加速度
accleration = list(map(lambda x, y: math.sqrt(x**2 + y**2), xaccleration, yaccleration))

# 判断速度的变化趋势
for i in range(0, len(speed1) - 1):
    if speed1[i] <= speed1[i + 1]:
        zhengfu.append(1)
    else:
        zhengfu.append(-1)
zhengfu.append(zhengfu[len(zhengfu) - 2])  # 最后一项与倒数第二项相同

# 计算特征值
feature_values = list(map(lambda x, y: x * y, accleration, zhengfu))
for i in range(0, len(feature_values) - 1):
    if i+1 in transform:
        continue
    acclerationdif.append(feature_values[i+1] -feature_values[i])
for i in range(0, len(x) - 1):
    if i+1 in transform:
        continue
    xavg.append((x[i]+x[i+1])/2)
for i in range(0, len(y) - 1):
    if i+1 in transform:
        continue
    yavg.append((y[i]+y[i+1])/2)
for i in range(0, len(time) - 1):
    if i+1 in transform:
        continue
    time1.append(time[i+1]-time[i])
acclerationdif = list(map(lambda x, y: x/y, acclerationdif, time1))
for i in range(len(xavg)):
    if (dotlinedistance(xavg[i],yavg[i],k1,b1)<0) and(dotlinedistance(xavg[i],yavg[i],k2,b2)>0):
        xpart.append(xavg[i])
        ypart.append(yavg[i])
        feature_valuespart.append(acclerationdif[i])
        realdistance.append(abs(dotlinedistance(xavg[i],yavg[i],k1,b1))*calculate_distance(y_3, x_3, y_2, x_2)/abs(dotlinedistance(x_3,y_3,k1,b1))*1000)
plt.scatter(realdistance, feature_valuespart, color='r', label='1')  # 红色的散点


# 添加标题、标签和图例
plt.title('jerk')
plt.xlabel('X ')
plt.ylabel('Y ')
plt.legend()  # 显示图例

# 显示图形
plt.show()